<template>
	<view class='contain'>
		<view class="up-image">
			<view class="up-rules" @click="goRules()">
				<text>使用规则</text>
			</view>
		</view>
		<block v-for="(item,index) in couponList" :key="index">
			<view class="down-quan">
				<view class="quan-box">
					<view class="quan-box-up">
						<view class="quan-box-up-money">
							<text>￥</text>
							<text>{{item.discountAmount}}</text>
						</view>
						<view class="quan-box-up-rules" v-if="item.couponForm==1">
							<text>满减券</text>
							<text v-if="item.useGoodsForm==1">全部商品 满{{item.satisfyAmount}}元可用</text>
							<!-- <text v-if="item.useGoodsForm==2">部分商品 满{{item.satisfyAmount}}元可用</text> -->
						</view>
						<view class="quan-box-up-rules" v-if="item.couponForm==2">
							<text>商品券</text>
							<text v-if="item.useGoodsForm==1">全部商品可用</text>
							<text v-if="item.useGoodsForm==2">部分商品可用</text>
						</view>
					</view>
					<view class="quan-box-down">
						<text v-if="item.validForm==1">有效期：{{item.validStartTimeYear}}.{{item.validStartTimeMonth}}.{{item.validStartTimeDay}}-{{item.validEndTimeYear}}.{{item.validEndTimeMonth}}.{{item.validEndTimeDay}}</text>
						<text v-if="item.validForm==2">有效期：领券之日算起，{{item.validDays}}天后过期</text>
					</view>
					<view class="quan-box-down-right-img">
						<image src="../../static/img/discount/red.png"></image>
						<view class="outer" @click="getCoupon(item)">
							<text>立即</text><text>领取</text>
						</view>
					</view>
				</view>
			</view>
		</block>
		<view v-if="noCoupon" class="noCoupon">
			<image src="../../static/img/discount/noCoupon.png"></image>
		</view>
		<ensure
			:isMutipleLine="false" 
			:visible.sync="promptVisible" 
			inputStyle="" 
			:title="title"
			mainColor="rgba(153,153,153,1)" 
			@confirm="clickPromptConfirm" 
			:ensure="cEnsure" 
			ensureColor="rgba(0,0,0,1)"
			titleColor="rgba(51,51,51,1)" 
			titleSize="36"
			fontWeight="500"
			enSurefontWeight="560"
		  >
		</ensure>
	</view>
</template>

<script>
	import ensure from "@/components/confirm/confirm.vue"
	export default {
		components:{
			ensure
		},
		data() {
			return {
				//优惠券列表
				couponList:[],
				//是否弹框
				promptVisible:false,
				//优惠券ID
				couponId:'',
				//弹框标题
				title:'',
				//是否新用户：0否1是
				isNew:'',
				//是否VIP：0否1是
				isVip:'',
				successNumber:'',
				//弹框右键文字设置
				cEnsure:'',
				//满足金额
				satisfyAmount:'',
				//优惠金额
				discountAmount:'',
				//(1-全部商品 2-部分商品)
				useGoodsForm:'',
				//(1-满减券 2-商品券)
				couponForm:'',
				//暂无优惠券
				noCoupon:false
			}
		},
		onLoad: function(options) {
			//获取优惠卷列表
			this.getCouponList()
			//判断用户是否是新用户/VIP用户/积分等级用户
			this.judgeMember()
		},
		methods: {
			//使用规则
			goRules: function() {
				uni.navigateTo({
					url: '/pages/rules/rules?rules=2',
					animationType: 'pop-in',
					animationDuration: 200
				});
			},
			//获取优惠卷列表
			getCouponList:function(){
				let params = {
					memberId: this.pub.getUserInfo().memberId,
					// memberId: this.pub.getUserInfo().memberId || "4f0abd1af84d4168ba27ac00b63fcb2e",
				}
				this.$postApi(this.$path.COUPON_LIST,params).then(res => {
					console.log("优惠卷列表------------>",res.data)
					if(res.data.success){
						this.couponList = res.data.data.list
						if(this.couponList.length!=0){
							this.couponList.forEach(res=>{
									let startTime = res.validStartTime.split(/[\s\n]/)
									startTime=startTime[0].split("-")
									res.validStartTimeYear = startTime[0]
									res.validStartTimeMonth = startTime[1]
									res.validStartTimeDay = startTime[2]
									
									let endTime = res.validEndTime.split(/[\s\n]/)
									endTime=endTime[0].split("-")
									res.validEndTimeYear = endTime[0]
									res.validEndTimeMonth = endTime[1]
									res.validEndTimeDay = endTime[2]
							})
						}else{
							this.noCoupon=true
						}
						console.log("this.couponList------------>",this.couponList)
					}else{
						this.noCoupon=true
						this.pub.showError(res.data.error.errorMessage)
					}
				})
			},
			//判断用户是否是新用户/VIP用户/积分等级用户
			judgeMember:function(){
				let params = {
					id: this.pub.getUserInfo().memberId,
					// id: this.pub.getUserInfo().memberId || "4f0abd1af84d4168ba27ac00b63fcb2e",
				}
				this.$postApi(this.$path.GET_MEMBERID,params).then(res => {
					console.log("判断用户类型返回结果------------>",res.data)
					if(res.data.success){
						this.isNew = res.data.data.isNew
						this.isVip = res.data.data.isVip
					}else{
						this.pub.showError(res.data.error.errorMessage)
					}
				})
			},
			//领取优惠卷
			getCoupon:function(item){
				
				switch(item.memberForm){
					// case "1":
					
					// 	break;
					//新用户优惠券
					case "2":
						if(this.isNew==0){
							this.pub.showError("新用户才能领取此优惠卷")
							return
						}
						break;
					//VIP用户优惠券
					case "3":
						if(this.isVip==0){
							this.pub.showError("VIP用户才能领取此优惠卷")
							return
						}
						break;
					//积分等级用户优惠券
					// case "4":
						
					// 	break;
					
					}
				this.satisfyAmount=item.satisfyAmount
				this.discountAmount=item.discountAmount
				this.useGoodsForm=item.useGoodsForm
				this.couponForm=item.couponForm
				
				let params = {
					memberId: this.pub.getUserInfo().memberId,
					// memberId: this.pub.getUserInfo().memberId || "4f0abd1af84d4168ba27ac00b63fcb2e",
					couponId:item.id
					
				}
				this.$postApi(this.$path.GET_COUPON,params).then(res => {
					console.log("领取优惠卷返回结果------------>",res.data)
					
					if(res.data.success){
						this.promptVisible = true
						this.couponId=res.data.data.couponId
						this.title = '恭喜您，领取成功'
						this.cEnsure = '去使用'
						this.successNumber = '1'
						this.getCouponList()
						//清除最后一张优惠券数据
						this.couponList = []
					}
				})
			},
			//弹窗成功
			clickPromptConfirm: function() {
				
				switch(this.successNumber){
					
					//领取成功
					case "1":
					this.promptVisible = false
					if(this.useGoodsForm=='1'){
						uni.redirectTo({
							url: '/pages/home_new/home_new',
							animationType: 'pop-in',
							animationDuration: 200
						});
					}else{
						uni.navigateTo({
							url: '/pages/discount/coupon_list?couponId='+this.couponId+'&satisfyAmount='+this.satisfyAmount+'&discountAmount='+this.discountAmount+'&couponForm='+this.couponForm,
							animationType: 'pop-in',
							animationDuration: 200
						});
					}
						break;
				}
			},

		}
	}
</script>

<style lang="scss">
	.contain{
		margin: 0;
		padding: 0;
		width: 100%;
		min-width: 100%;
		font-family:PingFang SC;
		background-color:rgba(255,255,255,1);
		.up-image{
			width: 100%;
			height: 286upx;
			background-image: url(../../static/img/discount/linquan-center.png);
			background-size: 100% 100%;
			margin-bottom: 28upx;
		}
		.up-rules{
			width:132upx;
			height:36upx;
			background:rgba(255,255,255,1);
			opacity:0.8;
			border-top-left-radius: 18upx;
			border-bottom-left-radius: 18upx;
			text-align: center;
			font-size:26upx;
			line-height: 36upx;
			font-weight:500;
			color:rgba(255,106,24,1);
			float: right;
			margin-top: 63upx;
		}
		.down-quan{
			padding:  0 24upx;
			position: relative;
			margin-bottom: 20upx;
		}
		.quan-box{
			border: 12upx solid rgba(255,230,214,1);
			border-radius:10upx;
			padding-left:41upx ;
		}
		.quan-box-up{
			width:446upx;
			height: 132upx;
			display: flex;
			align-items: center;
			border-bottom: 1upx solid rgba(179,179,179,1);
		}
		.quan-box-up-money{
			color:rgba(255,94,4,1);
		}
		.quan-box-up-money text:first-child{
			font-size:24upx;
			font-weight:500;
		}
		.quan-box-up-money text:last-child{
			font-size:76upx;
			font-weight:500;
		}
		.quan-box-up-rules{
			margin-left: 29upx;
			display: flex;
			flex-direction: column;
		}
		.quan-box-up-rules text:first-child{
			font-size:32upx;
			font-weight:bold;
			color:rgba(0,0,0,1);
			margin-bottom: 10upx;
		}
		.quan-box-up-rules text:last-child{
			font-size:26upx;
			font-weight:bold;
			color:rgba(102,102,102,1);
		}
		.quan-box-down{
			height: 71upx;
			display: flex;
			align-items: center;
			font-size:22upx;
			font-weight:400;
			color:rgba(77,77,77,1);
		}
		.quan-box-down-right-img{
			width: 195upx;
			height: 223upx;
			position: absolute;
			right: 25upx;
			top: 0upx;
		}
		.quan-box-down-right-img image{
			width: 100%;
			height: 100%;
			
			// background-image: url(../../static/img/discount/red.png);
			
		}
		.outer{
			width:136upx;
			height:136upx;
			border-radius:50%;
			border: 15upx solid rgba(252,137,114,0.6);
			background-color: #FFF4D0;
			font-size:32upx;
			font-weight:bold;
			color:rgba(255,74,71,1);
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			position: absolute;
			top: 47upx;
			right: 21upx;
		}
		.outer text{
			display: inline-block;
			width: 65upx;
			white-space: nowrap;
		}
		.noCoupon{
			height: 420upx;
			margin-top: 100upx;
		}
		.noCoupon image{
			width: 100%;
			height: 100%;
		}
	}
</style>
